const crypto = require('crypto');
module.exports={
    getToken(value) {
        return this.app.jwt.sign(value, this.app.config.jwt.secret);
    },
    formatTime(time){
        let d = new Date(time);  
        const Month = (d.getMonth() + 1) >= 10 ? (d.getMonth() + 1) : '0'+(d.getMonth() + 1)
        const Day = d.getDate() >= 10 ? d.getDate() : '0' + d.getDate()
        const h = d.getHours() >= 10 ? d.getHours() : '0' + d.getHours()
        const m = d.getMinutes() >= 10 ? d.getMinutes() : '0' + d.getMinutes()
        const s = d.getSeconds() >= 10 ? d.getSeconds() : '0' + d.getSeconds()
        return d.getFullYear() + '-' + Month + '-' + Day + ' ' + h + ':' + m + ':' + s;
    },
    checkToken(token) {
        return this.app.jwt.verify(token, this.app.config.jwt.secret);
    },
    apiSuccess(data,msg,code){
        this.body={msg,data,code};
        this.status=code
    },
    apiFail(data,msg,code){
        this.body={msg,data,code};
        this.status=code
    },
    genID(length) {
        return Number(Math.random().toString().substr(3, length) + Date.now()).toString(36);
    },
    randomString(length) {
        const chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        var result = '';
        for (var i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)];
        return result;
    },
}